<?php 

class App_Model_DbTable_Attachment_Shopping extends Ext_Db_Table  
{  
    protected $_name = 'attachment_shopping';
    
    private static $_shopping = array();
    
    public function getRowById($id)
    {
        if(!$id)
        {
                return "";
        }

        if(isset(self::$_shopping['id'][$id]))
        {
                return self::$_shopping['id'][$id];
        }

        $db = $this->getAdapter();
        $safe = $db->quoteInto(" id = ? ", $id);
        $row = $this->fetchRow($safe);
        if (!$row) {
            return "";
        }
        $_shopping = $row->toArray();

        self::$_shopping['id'][$id] = $_shopping;

        return $_shopping;
    }

    public function getAllById($idArray)
    {
        if(!$idArray)
        {
                return "";
        }

        $db = $this->getAdapter();
        $safe = $db->quoteInto(" id IN(?) ", $idArray);
        $all = $this->fetchAll($safe);
        if (!$all) {
                //throw new Exception("Count not find row $id");
                return "";
        }
        return $all->toArray();
    }

    public function del($id)
    {
        if(!$id)
        {
                return "";
        }

        $this->_unsetShoppingDataRow($id);

        $db = $this->getAdapter();
        $safe = $db->quoteInto(" id = ? ", $id);
        $rows_affected = $this->delete($safe);

        return $rows_affected;
    }

    public function addNew($inputArray)
    {
        $sql_data_array = array(
            'name'               => '',
            'description'        => '',
        );
        if(array_key_exists('articleId', $inputArray)) $sql_data_array['articleId'] = $inputArray['articleId'];
        if(array_key_exists('shoppingId', $inputArray)) $sql_data_array['shoppingId'] = $inputArray['shoppingId'];
        if(array_key_exists('name', $inputArray)) $sql_data_array['name'] = $inputArray['name'];
        if(array_key_exists('description', $inputArray)) $sql_data_array['description'] = $inputArray['description'];
        if(array_key_exists('descriptionHtmlOn', $inputArray)) $sql_data_array['descriptionHtmlOn'] = $inputArray['descriptionHtmlOn'];

        try {
            $rows_affected = $this->insert($sql_data_array);
            $dataId = $this->getAdapter()->lastInsertId();
        }catch (Exception $e){
            trigger_error($e->getMessage(), E_USER_WARNING);
        }

        return $dataId;	
    }

    public function addOld($inputArray, $id)
    {
        $sql_data_array = array();
        if(array_key_exists('name', $inputArray)) $sql_data_array['name'] = $inputArray['name'];
        if(array_key_exists('description', $inputArray)) $sql_data_array['description'] = $inputArray['description'];
        if(array_key_exists('descriptionHtmlOn', $inputArray)) $sql_data_array['descriptionHtmlOn'] = $inputArray['descriptionHtmlOn'];
        if(!$sql_data_array)
        {
                return "";
        }

        try {
            $db = $this->getAdapter();
            $where = $db->quoteInto(" id = ? ", $id);
            $rows_affected = $this->update($sql_data_array, $where);
            if($rows_affected){
                $this->_unsetShoppingDataRow($id);
            }
        }catch (Exception $e){
            trigger_error($e->getMessage(), E_USER_WARNING);
        }

        return "";
    }
	
    private function _unsetShoppingDataRow($id)
    {
        if(!$id)
        {
            return "";
        }
        
        $shopping = $this->getRowById($id);
        if(!$shopping)
        {
            return "";
        }
        
        unset(self::$_shopping['id'][$id]);
    }
}